<!--
* @author luguoxiang
* @date 2021-09-07
* 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <slot v-if="has()"></slot>
</template>
<script lang="ts" setup>
import { useRoute } from 'vue-router';
import { has, permitToReg, toStringArray } from '@/soar';
import { useAccountStore } from '@/store';

const props = defineProps({
  /**
   * 权限
   */
  permit: {
    type: [String, Array],
    required: true,
  },
});

const { permits } = useAccountStore();
const parent = useRoute().meta?.permit;
const regs = permits.map(permitToReg);

// eslint-disable-next-line no-redeclare
function has() {
  const values = toStringArray(props.permit).map((x) => (/^[a-zA-Z0-9].*$/.test(x) ? `${parent}/${x}` : x));
  for (const reg of regs) {
    for (const value of values) {
      if (reg.test(value)) {
        return true;
      }
    }
  }
  return false;
}
</script>
<script lang="ts">
export default {
  name: 'LgAuth',
};
</script>
